package com.bw.zg3park2212.house

import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.entity.MyHouseEntity
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.adpter.HouseAdpter
import com.bw.zg3park2212.databinding.ActivityHouseBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path="/app/HouseActivity")
class HouseActivity : BaseActivity<ActivityHouseBinding,HouseViewModel>() {
    lateinit var houseAdpter: HouseAdpter

    override fun intitView() {
        houseAdpter= HouseAdpter(viewmodel)
        binding.rvHouse.adapter=houseAdpter
        binding.rvHouse.layoutManager=LinearLayoutManager(this)
        binding.rbA.setOnClickListener {
            viewmodel.getHouse(1)
        }
        binding.rbB.setOnClickListener {
            viewmodel.getHouse(2)
        }
        binding.rbC.setOnClickListener {
            viewmodel.getHouse(3)
        }
        binding.rbD.setOnClickListener {
            viewmodel.getHouse(4)
        }
    }

    override fun intitData() {
        viewmodel.houseSuccse.observe(this){
            houseAdpter.data.clear()
            var myHouseEntity= mutableListOf<MyHouseEntity>()
           val map= it.groupBy {
                it.area//根据面积分组
            }
            val list = map.keys.sorted()//根据面积排序
            var index=1
            for (key in list) {
                myHouseEntity.add(MyHouseEntity(index,key,map.get(key)!!))
                index++
            }
            houseAdpter.addData(myHouseEntity)
            houseAdpter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        viewmodel.getHouse(1)
    }
}